var institutionservice = require("../../../apiservice/institution/institutionservise");

// pages/institution/add/add.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		region: ['湖南省', '长沙市', '芙蓉区'],
		userinfo: [],
		name: '',
    //isJoinedIndex:0,
    medicalTypeData: [{ "id": 1, "name": "社区卫生服务中心" }, { "id": 2, "name": "社区卫生服务站" }, { "id": 3, "name": "乡镇卫生院" }, { "id": 4, "name": "医院" }, { "id": 5, "name": "村卫生室" }, { "id": 6, "name": "其他" }],
    medicalTypeIndex: 0,
    medicalTypeName: "请选择",
    joinedType: [{ "id": 0, "name": "是" }, { "id": 1, "name": "否" }],
    joinedTypeIndex: 0,
    joinedTypeName: "是",
    showDistrict:false
	},


	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		if(options.province&&options.city&& options.area){
		  this.setData({     
		    region : [options.province,options.city, options.area]
		  })
		}
	},
	// 位置监听事件
	bindRegionChange: function(e) {
		// this.setData({
		// 	region: e.detail.value,
		// });
		//console.log(this.data.competitorlist)
		//this.GetStreetByPCA(this.data.region[0],this.data.region[1],this.data.region[2]);
    console.log('picker发送选择改变，携带值为', e.detail.currentTarget.dataset);
    this.setData({
      showDistrict: false
    });
    var provinceName = e.detail.currentTarget.dataset.province;
    var cityName = e.detail.currentTarget.dataset.city;
    var areaName = e.detail.currentTarget.dataset.area;
    var region = [];
    region.push(provinceName);
    region.push(cityName);
    region.push(areaName);
    this.setData({
      region: region
    })
	},
	onChange: function(e) {
		this.setData({
			name: e.detail
		});
	},

  districtTap: function (e) {
    this.setData({ showDistrict: true });
  },
	//提交
	onSure: function(e) {
		if (this.data.name.replace(/\s+/g, '') == '') {
			wx.showToast({
				title: '请输入医疗机构名称',
				icon: 'none',
				duration: 2000
			});
      return;
		} 
    if (this.data.joinedTypeName == "请选择") {
      wx.showToast({
        title: '请选择是否医联体',
        icon: 'none',
        duration: 2000
      });
      return;
    }

    if (this.data.medicalTypeName=="请选择"){
      wx.showToast({
        title: '请选择医疗机构类型',
        icon: 'none',
        duration: 2000
      });
      return;
    }


			var competitor = {
				name: this.data.name,
				new_provincename: this.data.region[0],
				new_marketname: this.data.region[1],
				new_districtcountyname: this.data.region[2],
        new_type: this.data.medicalTypeData[this.data.medicalTypeIndex].id,
        new_iscoopmedicalcouplet: this.data.joinedTypeIndex==0?true:false
      }
			
			this.AddCompetitor(competitor);
		
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		let userinfo = wx.getStorageSync('useridentity');
		this.setData({
			userinfo: userinfo
		});
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},

  // onIsJoinedChange:function(e){
  //   this.setData({
  //     isJoinedIndex: e.detail.index,
  //   });
  // },
  changeJoinedType: function (e) {
    var index = e.detail.value;
    this.setData({
      joinedTypeIndex: index,
      joinedTypeName: this.data.joinedType[index].name
    });

  },
  changeMedicalType:function(e){
    var index = e.detail.value;
    this.setData({
      medicalTypeIndex: index,
      medicalTypeName: this.data.medicalTypeData[index].name
    });
  },

	// 根据医疗机构ID获取医疗机构信息
	AddCompetitor: function(obj) {
		var that = this;
		var msg = '创建';
		if (obj.competitorid) {
			msg = '修改';
		}
    wx.showLoading({
      title: '提交中',
      mask: true
    })
		institutionservice.AddCompetitor(obj).then(function(success) {
			that.showResult(success.data, msg);
			wx.navigateBack({
				delta: 1
			})
		}).catch(function(ex) {
			console.log(ex);
		})
	},
	showResult: function(res, msg) {
		var title = '';
		var icon = '';
		if (res.resultState == 1) {
			title = msg + '成功';
			icon = 'success';
		} else {
			title = msg + '失败';
			icon = 'none';
		}
		wx.showToast({
			title: title,
			icon: icon,
			duration: 2000
		});
	},
})
